include("${ClickHouse_SOURCE_DIR}/cmake/dbms_glob_sources.cmake")
add_headers_and_sources(clickhouse_functions_url .)
add_library(clickhouse_functions_url OBJECT ${clickhouse_functions_url_sources} ${clickhouse_functions_url_headers})
target_link_libraries(clickhouse_functions_url PRIVATE dbms)
set_source_files_properties(tldLookup.generated.cpp PROPERTIES COMPILE_FLAGS -Wno-shorten-64-to-32)

if (OMIT_HEAVY_DEBUG_SYMBOLS)
    target_compile_options(clickhouse_functions_url PRIVATE "-g0")
endif()

# TODO: move Functions/Regexps.h to some lib and use here
if (TARGET ch_contrib::vectorscan)
    target_link_libraries(clickhouse_functions_url PRIVATE ch_contrib::vectorscan)
    list (APPEND PRIVATE_LIBS ch_contrib::vectorscan PARENT_SCOPE)
endif()

if (USE_GPERF)
    # Only for regenerate
    add_custom_target(generate-tldlookup-gperf ./tldLookup.sh
        SOURCES tldLookup.sh
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/tldLookup.gperf"
    )
    add_custom_target(generate-tldlookup ${GPERF} tldLookup.gperf --output-file=tldLookup.generated.cpp && clang-format -i tldLookup.generated.cpp
        #  apply these clang-tidy fixes to the file generated by gperf:
        # - use cstring since string.h is deprecated
        # - return nullptr instead of 0
        && sed -i -e 's/\#include <string.h>/\#include <cstring>/g' tldLookup.generated.cpp -e 's/return 0\;/return nullptr\;/g' tldLookup.generated.cpp

        SOURCES tldLookup.gperf
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        # BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/tldLookup.generated.cpp"
    )
    add_dependencies(generate-tldlookup generate-tldlookup-gperf)
    if(NOT TARGET generate-source)
        add_custom_target(generate-source)
    endif()
    add_dependencies(generate-source generate-tldlookup)
endif ()
